//用户地址
(function(){
	var db = require("../db/index");
	var config = require("../_include/config");
	var db_table_name = config.dbname.userAddress;
	var db_id_prefix = config.dbIdPrefix.userAddress;


	//构造函数
	var _con = function (info,isAnonymous){
		var self = this;
		//ID编号
		self._id = db_id_prefix+Math.random().toString(16).substr(2)+(+new Date).toString(16);
		if (!(info.province&&info.town&&info.county)) {
			throw "收获地址:所在地区信息不完整";
		}
		//PS:详见淘宝的归类方式
		//省级
		self.province = info.province;
		//市级
		self.town = info.town;
		//县级
		self.county = info.county;
		//街道（可选）
		self.street = info.street;

		if (!info.postcode) {
			throw "收获地址:邮编为空";
		}
		//邮编
		self.postcode = info.postcode;

		if (!info.detail) {
			throw "收获地址:街道地址为空";
		}
		//街道地址详情
		//length(5~120)
		self.detail = info.detail;

		if (!info.name) {
			throw "收获地址:收货人姓名为空";
		}
		//收货人姓名
		//length(1~25)
		self.name = info.name;

		if (!(info.mobilenumber||self.phonenumber)) {
			throw "收获地址:手机号码或者电话号码必填一项";
		}
		//手机号码（与座机号码必填一项）
		self.mobilenumber = info.mobilenumber;
		//座机号码
		self.phonenumber = info.phonenumber;

		//保存地址信息
		db.insert(db_table_name,self);
		console.log("生成了一个用户地址：",self._id);
	};
	//PS:模块的暴露放在require skill前面，因为二者互相依赖
	module.exports = _con;
	//技能
	_con.prototype = require("./skill");

	//静态方法
	_con.get = function(Id){
		var result = db.find_by_id(db_table_name,Id);
		console.log("result",result);
		if(result){
			result.__proto__ = _con.prototype;
		}
		return result;
	};
}());
